from pymongo import *
from datetime import *

mySet = None


def noteFunc():
    strData = input("请输入录入的信息\n")
    strTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    writeDB(strTime, strData)


def findFunc():
    flag = input("请选择，时间查询输1，内容查询输入2")
    data = input("请输入关键字\n")
    if "1" == flag:
        dictData = {'time': {'$regex': data}}
    elif "2" == flag:
        dictData = {'content': {'$regex': data}}
    else:
        print("输入错误")
    query = mySet.find(dictData, {'time': 1, 'content': 1, '_id': 0})
    # db.myset.find({time:{$regex:/2018/}})
    for data in query:
        print(data)
    print("OK")


def writeDB(sTime, sData):
    post = {'time': sTime, 'content': sData}
    mySet.insert(post)


def readDB():
    query = mySet.find({}, {'time': 1, 'content': 1, '_id': 0})
    print("数据库信息：")
    for data in query:
        print(data)


def init():
    global mySet
    client = MongoClient("localhost", 27017)
    note = client.mydb
    mySet = note.myset
    print("ddd")


def main():
    init()
    while True:
        choose = input("选择，输入note记录信息，输入find查询信息，输入exit退出\n")
        if "note" == choose:
            noteFunc()
            readDB()
        elif "find" == choose:
            findFunc()
        elif "exit" == choose:
            break
        else:
            print("输入错误，请重新输入")


# if __name__ == '__main__':
main()
